package cn.tx.controller;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import cn.tx.model.Emp;
import cn.tx.model.Role;
import cn.tx.query.RoleQuery;
import cn.tx.service.EmpService;
import cn.tx.service.RoleService;
import cn.tx.util.JSONWriter;
import cn.tx.util.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class RoleAction extends BaseAction implements ModelDriven<Role>{
	
	private Role role = new Role();
	
	private RoleService roleService;
	
	private EmpService empService;
	
	private String menuIds;
	
	
	private String ids;
	
	

	public String getMenuIds() {
		return menuIds;
	}

	public void setMenuIds(String menuIds) {
		this.menuIds = menuIds;
	}

	/**
	 * 查询对象
	 */
	private RoleQuery erpQuery = new RoleQuery();
	
	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public EmpService getEmpService() {
		return empService;
	}

	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}



	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}
	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	

	public RoleQuery getErpQuery() {
		return erpQuery;
	}

	public void setErpQuery(RoleQuery erpQuery) {
		this.erpQuery = erpQuery;
	}

	/**
	 * 查询员工的列表
	 * @return
	 */
	public String role_list(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		Page page = roleService.selectObjectByConditionPage(erpQuery, exclude);
		
		context.put("page", page);
		
		return SUCCESS;
	}
	
	public String role_poplist(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		List<Role> list = roleService.selectGrantRole(erpQuery, exclude);
		context.put("rlist", list);
		
		return SUCCESS;
	}
	public void role_grant(){
		roleService.updateGrantRole(erpQuery.getEmpId(), ids);
		try {
			response.getWriter().write("yes");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public void role_grantPerm(){
		roleService.updateGrantPerm(role.getRoleId(), menuIds);
		JSONWriter.writeString("yes", response);
		
	}
	
	
	
	/**
	 * 跳转到添加页面
	 * @return
	 */
	public String role_input(){
		
		return SUCCESS;
	}
	/**
	 * 跳转到修改页面
	 * @return
	 */
	public String role_preupdate(){
		role = roleService.getObjectById(role.getRoleId());
		return SUCCESS;
	}
	/**
	 * 保存
	 * @return
	 */
	public String role_save(){
		roleService.save(role);
		return TOLIST;
	}
	/**
	 * 修改
	 * @return
	 */
	public String role_update(){
		roleService.update(role);
		return TOLIST;
	}
	/**
	 * 修改
	 * @return
	 */
	public String role_delete(){
		role = roleService.getObjectById(role.getRoleId());
		roleService.delete(role);
		return TOLIST;
	}

	@Override
	public Role getModel() {
		return role;
	}
	
	
	
	

}
